﻿using Md.Api.Entity.DapperModels.MdDB;

namespace Md.Dapper.Repositories.Md;
/// <summary>
/// 系统岗位仓储
/// </summary>
public class SysPostRep : BaseRep<SysPostModel>
{
    /// <summary>
    /// 表字段
    /// </summary>
    private static string sqlFields = string.Format(@"
         `post_id`
        ,`post_code`
        ,`post_name`
        ,`post_sort`
        ,`status`
        ,`create_by`
        ,`create_time`
        ,`update_by`
        ,`update_time`
        ,`remark`
    ");

    /// <summary>
    /// 通过岗位编号获取系统岗位信息
    /// </summary>
    /// <param name="postId">岗位编号</param>
    /// <returns>SysPostModel</returns>
    public SysPostModel? GetSysPostByPostId(long postId)
    {
        string sqlWhere = " and `post_id` = @PostId ";

        return QuerySingle(sqlFields, sqlWhere, new { PostId = postId });
    }

    /// <summary>
    /// 获取系统岗位列表数据
    /// </summary>
    /// <param name="model">查询条件</param>
    /// <param name="orderBy">排序字段</param>
    /// <param name="isDesc">是否倒序</param>
    /// <returns>IEnumerable<SysPostModel></returns>
    public IEnumerable<SysPostModel> GetSysPostList(SysPostModel model, string orderBy, bool isDesc)
    {
        string sqlWhere = string.Empty;

        #region ===where===
        if (!string.IsNullOrWhiteSpace(model.PostCode))
        {
            sqlWhere += " and post_code like concat('%', @PostCode ,'%') ";
        }
        if (!string.IsNullOrWhiteSpace(model.PostName))
        {
            sqlWhere += " and post_name like  concat('%', @PostName ,'%') ";
        }
        if (!string.IsNullOrWhiteSpace(model.Status))
        {
            sqlWhere += " and status = @Status ";
        }
        if (!string.IsNullOrWhiteSpace(model.CreateBy))
        {
            sqlWhere += " and create_by = @CreateBy ";
        }
        if (!string.IsNullOrWhiteSpace(model.UpdateBy))
        {
            sqlWhere += " and update_by = @UpdateBy ";
        }

        sqlWhere += GetOrderByDesc(orderBy, isDesc);
        #endregion

        return QueryList(sqlFields, sqlWhere, model);
    }

    /// <summary>
    /// 分页获取系统岗位列表数据
    /// </summary>
    /// <param name="model">查询条件</param>
    /// <param name="pageIndex">当前页</param>
    /// <param name="pageSize">每页条数</param>
    /// <param name="orderBy">排序字段</param>
    /// <param name="isDesc">是否倒序</param>
    /// <returns>Page<SysPostModel></returns>
    public Page<SysPostModel> GetSysPostPageList(SysPostModel model, int pageIndex, int pageSize, string orderBy, bool isDesc)
    {
        string sqlWhere = string.Empty;

        #region ===where===
        if (!string.IsNullOrWhiteSpace(model.PostCode))
        {
            sqlWhere += " and post_code like concat('%', @PostCode ,'%') ";
        }
        if (!string.IsNullOrWhiteSpace(model.PostName))
        {
            sqlWhere += " and post_name like  concat('%', @PostName ,'%') ";
        }
        if (!string.IsNullOrWhiteSpace(model.Status))
        {
            sqlWhere += " and status = @Status ";
        }
        if (!string.IsNullOrWhiteSpace(model.CreateBy))
        {
            sqlWhere += " and create_by = @CreateBy ";
        }
        if (!string.IsNullOrWhiteSpace(model.UpdateBy))
        {
            sqlWhere += " and update_by = @UpdateBy ";
        }

        sqlWhere += GetOrderByDesc(orderBy, isDesc);
        #endregion

        return QueryPage(sqlFields, sqlWhere, model, pageIndex, pageSize);
    }

    /// <summary>
    /// 通过岗位编号更新系统岗位
    /// </summary>
    /// <param name="model">请求实体</param>
    /// <returns>int</returns>
    public long UpdateSysPostByRoleId(SysPostModel model)
    {
        string sqlSet = string.Empty;

        #region ===set===
        if (!string.IsNullOrWhiteSpace(model.PostCode))
        {
            sqlSet += " `post_code` = @PostCode, ";
        }

        if (!string.IsNullOrWhiteSpace(model.PostName))
        {
            sqlSet += " `post_name` = @PostName ";
        }

        if (model.PostSort > -1)
        {
            sqlSet += " `post_sort` = @PostSort, ";
        }

        if (!string.IsNullOrWhiteSpace(model.Status))
        {
            sqlSet += " `status` = @Status, ";
        }

        if (!string.IsNullOrWhiteSpace(model.UpdateBy))
        {
            sqlSet += " `update_by` = @UpdateBy, ";
        }

        if (model.Remark != null)
        {
            sqlSet += " `remark` = @Remark, ";
        }

        sqlSet += " `update_time` = sysdate() ";
        #endregion

        string sqlWhere = " and `post_id` = @PostId ";

        return Update(sqlSet, sqlWhere, model);
    }

    /// <summary>
    /// 通过系统岗位编号删除系统岗位
    /// </summary>
    /// <param name="postId">系统岗位编号</param>
    /// <returns>int</returns>
    public int DeleteSysPostByPostId(long postId)
    {
        string sqlWhere = " and `post_id` = @PostId ";

        return Delete(sqlWhere, new { PostId = postId });
    }
}

